<?php
namespace app\user\controller;
use app\common\model\WebUser;
use think\facade\Config;
use think\facade\App;
use think\facade\Log;
use think\facade\Session;
use ZipArchive;
use think\Db;
use app\common\model\Market as MarketModel;
use app\common\model\LoginLog;
use app\common\model\App as AppModel;
/**
 * 后台首页
 * Class Index
 * @package app\user\controller
 */
class Index extends Controller
{
    /**
     * 后台首页
     * @return mixed
     */
    public function index()
    {
        $model = new MarketModel;
        $data = $model->getHomeData();
		
        $log=(new LoginLog)->getAll($this->request->param());
		
		$version=AppModel::getAppCache();
        return $this->fetch('index', compact('data','version','log'));
    }
	
	/**
     * 意见箱
     * @return mixed
     */
    public function proposal()
    {
        
		return $this->fetch('proposal');
		
        
    }
	
    public function Cross()
    {
    }
	
	 /**
     * 在线升级
     * @return mixed
     */
	public function update()	
	{
	}

	 /**
     * 获取文件
     * @return mixed
     */
	function getFile($url, $filename,$save_dir,$type = 0) {
		$filename=$filename.'.zip';
		//获取远程文件所采用的方法
		ob_start();
		readfile($url);
		$content = ob_get_contents();
		ob_end_clean();
		$fp2 = @fopen($save_dir . $filename, 'a');
		fwrite($fp2, $content);
		fclose($fp2);
		unset($content, $url);
		return array(
			'file_name' => $filename,
			'save_path' => $save_dir . $filename
		);
	}

	public function add(){
        return $this->fetch('index1');
    }

    public function switch_app($id)
    {
        Log::notice("switch_app");
        $where=[
            'user_name' => $this->store['user_name'],
            'is_delete' => 0,
            'app_id' => [0,$id],
        ];

        $user_info = WebUser::useGlobalScope(false)->where($where)->find();

        Log::notice($user_info);

        if (empty($user_info)) {
            return $this->renderError('权限不足');
        }
        //当前用户
        Session::set('current_store', $user_info);

        //当前app_id
        Session::set('app_id',$id);

        return true;
    }
}